package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

//测试jdbc
public class TestJDBC {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/jdbctest"
                +"?characterEncoding=utf8" //防止中文乱码
                +"&useSSL=false" //高版本的jar需要忽略权限
                +"&serverTimezone=Asia/Shanghai";//高版本的jar需要
        String name="root";
        String pwd="root";
        //获取和数据库的连接
        Connection conn = DriverManager.getConnection(url,name,pwd);
        //获取传输器,查询user表的所有数据
        String sql = "select * from user";
        PreparedStatement ps = conn.prepareStatement(sql);
        //执行SQL
        ResultSet rs = ps.executeQuery();
        //解析结果集
        while(rs.next()){
            for (int i = 1; i <= 3 ; i++) {
                System.out.println( rs.getString(i) );
            }
        }
        //释放资源
        rs.close();
        ps.close();
        conn.close();
    }
}
